﻿using System;
using System.Web.UI.Design;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Text;

namespace TPG.SharePoint.Workflow.Controls.Design
{
    class DesignerRegionHelper
    {
        internal static string GetTemplatedEditableDesignerRegionContent(
            IDesignerHost host, TemplatedEditableDesignerRegion templatedRegion)
        {
            TemplateDefinition definition = templatedRegion.TemplateDefinition;
            PropertyDescriptor templateProperty = TypeDescriptor.GetProperties(definition.TemplatedObject)[definition.TemplatePropertyName];
            ITemplate template = (ITemplate)templateProperty.GetValue(definition.TemplatedObject);
            string regionContent = null;
            if (template != null)
            {
                regionContent = ControlPersister.PersistTemplate(template, host);
            }
            return regionContent;
        }

        internal static void SetTemplatedEditableDesignerRegionContent(
            IDesignerHost host, TemplatedEditableDesignerRegion templatedRegion, string content)
        {
            TemplateDefinition definition = templatedRegion.TemplateDefinition;
            PropertyDescriptor templateProperty = TypeDescriptor.GetProperties(definition.TemplatedObject)[definition.TemplatePropertyName];
            ITemplate template = ControlParser.ParseTemplate(host, content);
            using (DesignerTransaction transaction = host.CreateTransaction())
            {
                templateProperty.SetValue(definition.TemplatedObject, template);
            }
        }

        internal static void ConvertToTemplate(
            IDesignerHost host, string templateName, Control component, string templateContainerID)
        {
            ControlDesigner.InvokeTransactedChange(component,
                delegate(object context)
                {
                    PropertyDescriptor descriptor =
                        TypeDescriptor.GetProperties(component)[templateName];
                    ITemplate template = GetTemplateFromDesignModeState(host, component, templateContainerID);
                    descriptor.SetValue(component, template);
                    return true;
                },
                null, null);
        }

        internal static void ResetTemplate(string templateName, Control component)
        {
            ControlDesigner.InvokeTransactedChange(component,
                delegate(object context)
                {
                    PropertyDescriptor descriptor =
                        TypeDescriptor.GetProperties(component)[templateName];
                    descriptor.ResetValue(component);
                    return true;
                },
                null, null);
        }

        static ITemplate GetTemplateFromDesignModeState(IDesignerHost host, 
            Control component, string templateContainerID)
        {
            IDictionary designModeState = ((IControlDesignerAccessor)component).GetDesignModeState();
            StringBuilder templateText = new StringBuilder();
            Control control = designModeState[templateContainerID] as Control;
            foreach (Control contentControl in control.Controls)
            {
                templateText.Append(ControlPersister.PersistControl(contentControl, host));
            }
            return ControlParser.ParseTemplate(host, templateText.ToString());
        }
    }
}
